package com.woniuxy.house.mapper;

import com.woniuxy.house.entity.Booking;
import com.woniuxy.house.provider.BookingSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 预约看房表表mapper
 */
public interface BookingMapper {

    /**
    * 增加预约看房表
    */
    @Insert("insert into house_booking(id,booking_time,look_time,name,telephone,status,user_id,source_id) values(#{id},#{bookingTime},#{lookTime},#{name},#{telephone},#{status},#{user.id},#{source.id})")
    void addBooking(Booking booking);

    /**
     * 修改预约看房表
     */
    @UpdateProvider(type = BookingSqlProvider.class,method = "update")
    void updateBooking(Booking booking);

    /**
     * 删除预约看房表
     */
    @Delete("DELETE FROM house_booking WHERE id=#{id}")
    void deleteBooking(Integer id);

    /**
     * 查询全部的预约看房表
     */
    @Select("select * from house_booking")
    @Results(id = "result",value = {
            @Result(column = "id",property = "id",id = true),
            @Result(column = "booking_time",property = "bookingTime"),
            @Result(column = "look_time",property = "lookTime"),
            @Result(column = "name",property = "name"),
            @Result(column = "telephone",property = "telephone"),
            @Result(column = "status",property = "status"),
            @Result(column = "user_id",property = "user.id"),
            @Result(column = "source_id",property = "source",one = @One(select = "com.woniuxy.house.mapper.SourceMapper.getById",fetchType = FetchType.LAZY))

    })
    List<Booking> getAll();

    /**
     * 根据用户id查询的预约看房表
     */
    @Select("select * from house_booking where user_id=#{id}")
    @ResultMap(value = "result")
    List<Booking> getByUserId(Integer userId);

    /**
     * 根据房源id查询的预约看房表
     */
    @Select("select * from house_booking where source_id=#{id}")
    @ResultMap(value = "result")
    List<Booking> getBySourceId(Integer sourceId);
}
